<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-11 14:51
// +----------------------------------------------------------------------
namespace Admin\Controller;
class OrderBorrowController extends BaseController {

    public function index() {
        $mapstr='a.type=1 and a.state_order in (1,2) ';
        $order_id = I('get.order_id');
        if (!empty($order_id)) {
            $mapstr.=' and a.order_id = '.$order_id;
        }
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            if($mapstr!=''){
                $mapstr.=' and ';
            }
            $mapstr.='(a.order_id like "%'.$keywords.'%" or card_id="'.$keywords.'")';
        }
        $count = D('OrderBook')->alias('a')
            ->join('card on a.user_id=card.user_id','left')
            ->where($mapstr)
            ->field('a.*,card.card_id')
            ->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[ $key ] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = D('OrderBook')->alias('a')->relation(TRUE)//            ->join('left join card b on a.user_id=b.user_id')
        ->join('card on a.user_id=card.user_id','left')
            ->join('order_delivery on a.order_id=order_delivery.order_id','left')
            ->where($mapstr)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->field('a.*,card.card_id,order_delivery.content as delivery_content,order_delivery.type as delivery_type,order_delivery.state_order as delivery_state_order')
            ->order('a.create_time desc')->select();
        //        dump($data);
        //        die;
        foreach ($data as $k => $v) {
            $data[ $k ]['address'] = json_decode($v['address'] , TRUE);
            $data[$k]['delivery_content']=json_decode($v['delivery_content'] , TRUE);
            $data[$k]['product'] = M('order_has_product')->where(['order_id' => $v['order_id']])
                ->join('product on product.product_id = order_has_product.product_id')->select();
        }
        $this->data = $data;
        $this->display();
    }

    function add() {
        $delivery_man = M("Delivery_man");
        $dman = $delivery_man->field("man_id,name,phone")->select();
        $this->assign("dman" , $dman);
        $this->display("edit");
    }

    function edit() {
        $delivery_man = M("Delivery_man");
        $dman = $delivery_man->field("man_id,name,phone")->select();
        $order_id = I("get.order_id");
        $Odelivery = M("Order_delivery");
        $d = $Odelivery->where("order_id={$order_id}")->find();
        $content = json_decode($d['content']);
        $d['content'] = array ();
        $d['content']['name'] = $content->name;
        $d['content']['order_num'] = $content->order_num;
        $this->assign("d" , $d);
        $this->assign("dman" , $dman);
        $this->display();
    }

    function doEdit() {
        $type = I("post.type");
        $order_id = I("get.order_id");
        $Odelivery = M("Order_delivery");
        $count = $Odelivery->where("order_id={$order_id}")->count();
        $data['type'] = $type;
        $data['state_order'] = 2;
        switch ($type) {
            case 1:
                $data['man_id'] = I("post.man_id");
                $dc['name'] = I("post.name");
                $dc['phone'] = I("post.phone");
                break;
            case 2:
                $dc['name'] = I("post.delivery_name");
                $dc['order_num'] = I("post.order_num");
                break;
            default:
                break;
        }
        $data['content'] = json_encode($dc , JSON_UNESCAPED_UNICODE);
        if ($count > 0) {
            //修改发货信息
            $Odelivery->startTrans();
            $d = $Odelivery->where("order_id={$order_id}")->data($data)->save();
            if ($d) {
                $Odelivery->commit();
                $this->success("修改成功" , U("index"));
            } else {
                $Odelivery->rollback();
                $this->error("修改失败");
            }
        } else {
            $data['order_id'] = $order_id;
            //添加发货信息
            $Odelivery->startTrans();
            $delivery_id = $Odelivery->data($data)->add();
            //更改订单状态
            $Order = M("Order_book");
            $r = $Order->where("order_id={$order_id} && state_order=1")->data("state_order=2")->save();
            if ($delivery_id && $r) {
                $Odelivery->commit();
                $this->success("发货成功" , U("index"));
            } else {
                $Odelivery->rollback();
                $this->success("发货失败");
            }
        }
    }
}